package com.example.nftapp.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.nftapp.R;
import com.example.nftapp.beans.Product;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

public class MarketProductAllAdapter extends RecyclerView.Adapter<MarketProductAllAdapter.MVHolder> {

    private final List<Product> datas;

    public MarketProductAllAdapter(List<Product> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public MarketProductAllAdapter.MVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_rv_product, parent, false);
        return new MVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MarketProductAllAdapter.MVHolder holder, int position) {
        Product product = datas.get(position);
        holder.listShowItemNum.setText(String.format("%02d", position + 1));
        holder.listShowItemImg.setImageResource(product.getResId());
        holder.tvItemProductName.setText(product.getProductName());
        holder.tvItemProductPrice.setText("¥" + product.getProductPrice());
        BigDecimal bd = new BigDecimal(product.getPriceChange());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (bd.doubleValue() > 0) {
            holder.tvItemProductPriceChange.setText("+" + decimalFormat.format(bd.doubleValue() * 100) + "%");
            holder.tvItemProductPriceChange.setTextColor(Color.RED);
        } else {
            holder.tvItemProductPriceChange.setText(decimalFormat.format(bd.doubleValue() * 100) + "%");
            holder.tvItemProductPriceChange.setTextColor(Color.GREEN);
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class MVHolder extends RecyclerView.ViewHolder {
        TextView listShowItemNum;
        ImageView listShowItemImg;
        TextView tvItemProductName;
        TextView tvItemProductPrice;
        TextView tvItemProductPriceChange;

        public MVHolder(@NonNull View itemView) {
            super(itemView);
            listShowItemNum = itemView.findViewById(R.id.list_show_item_num);
            listShowItemImg = itemView.findViewById(R.id.list_show_item_img);
            tvItemProductName = itemView.findViewById(R.id.tvItemProductName);
            tvItemProductPrice = itemView.findViewById(R.id.tvItemProductPrice);
            tvItemProductPriceChange = itemView.findViewById(R.id.tvItemPriceChange);
        }
    }
}